import java.awt.Point;
import java.io.Serializable;
import java.util.Observable;

//เธ�เธฃเธฐเธ”เธฒเธ�เธ�เธญเธ�เธ•เธฑเธงเน€เธ�เธกเธ—เธตเน�เน€เธญเธฒเน�เธซเน�UI observ
public class GameBoard extends Observable implements Serializable {
	private Block[][] block;
	public final OColor BLACK_TURN;
	public final OColor WHITE_TURN;
	private static OColor state;
	private final CheckMove cm;

	public GameBoard() {
		cm = new CheckMove();
		BLACK_TURN = OColor.BLACK;
		WHITE_TURN = OColor.WHITE;
		block = new Block[8][8];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				block[i][j] = new Block();
			}
		}
		// 4 เธ�เน�เธญเธ�เธ•เธฃเธ�เธ�เธฅเธฒเธ�
		block[3][3].setColor(OColor.WHITE);
		block[3][4].setColor(OColor.WHITE);
		block[4][3].setColor(OColor.BLACK);
		block[4][4].setColor(OColor.BLACK);
		
		setBlack();
		//System.out.println(getState());
		//setChanged();
		//System.out.println(getState());
		//notifyObservers();
	}

	// เน€เธ�เธฅเธตเน�เธขเธ�เธชเธตเธ�เธฒเธงเธ”เธณ
	public void setColor(Point p, OColor c) {
		
		block[p.x][p.y].setColor(c);
		//System.out.println("setcolor");
		setChanged();
		notifyObservers();
	}
	
	public void setBlack(){
		state = BLACK_TURN;
		//System.out.println(countObservers());
	}

	// เธ”เธนเธชเธต
	public OColor getColor(Point p) {
		if (p.x>=0 && p.y>=0)
		return block[p.x][p.y].getColor();
		return null;
	}

	public CheckMove getCheckMove() {

		return cm;
	}

	public OColor getState() {
		return state;
	}

	// เน€เธ�เธฅเธตเน�เธขเธ�stateเธ�เน�เธฒเธ�เธฐbugเธ•เธญเธ�เธ—เธตเน�เธซเธฒเธ—เธตเน�เธฅเธ�เน�เธกเน�เน�เธ”เน�เน�เธกเน�เน€เธ�เธฅเธตเน�เธขเธ�stateเน€เธ”เธงเธ�เน�เธญเธขเน�เธ�เน�
	public void setState() {
		
		if (state == WHITE_TURN)
			state = BLACK_TURN;
		else if (state == BLACK_TURN)
			state = WHITE_TURN;
		
		System.out.println("next turn : " + state);
		//System.out.println(state);
		//System.out.println(countObservers());
		 setChanged();
		 notifyObservers();
		 //System.out.println("notify");
	}
}
